package com.sumer.brain.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumer.brain.common.annotation.UserId;
import com.sumer.brain.common.model.R;
import com.sumer.brain.sys.model.entity.SysMenu;
import com.sumer.brain.sys.model.req.SysMenuReq;
import com.sumer.brain.sys.model.resp.SysMenuResp;
import com.sumer.brain.sys.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Tag(name = "菜单管理")
@RequestMapping("/sysMenu")
public class SysMenuController {
    
    private final SysMenuService sysMenuService;

    @Operation(summary = "根据父级菜单id获取菜单集合", description = "根据父级菜单id获取菜单集合", tags = "查询")
    @GetMapping("getMenuByParentId")
    public R<List<SysMenu>> getMenuByParentId(@RequestParam(value = "parentId") Long parentId) {
        LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper = ObjectUtils.isEmpty(parentId) ?
                new LambdaQueryWrapper<SysMenu>().isNull(SysMenu::getParentId) :
                new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getParentId, parentId);
        return R.ok(sysMenuService.getBaseMapper().selectList(sysMenuLambdaQueryWrapper));
    }

    @Operation(summary = "分页查询菜单列表", description = "分页查询菜单列表", tags = "查询")
    @PostMapping("page")
    public R<Page<SysMenu>> page(@RequestBody SysMenuReq sysMenuReq) {
        Page<SysMenu> page = new Page<>(sysMenuReq.getPageNum(), sysMenuReq.getPageSize());
        LambdaQueryWrapper<SysMenu> lambdaQueryWrapper = new LambdaQueryWrapper<SysMenu>()
                .eq(StringUtils.hasText(sysMenuReq.getMenuName()), SysMenu::getMenuName, sysMenuReq.getMenuName())
                .eq(sysMenuReq.getStatus() != null, SysMenu::getStatus, sysMenuReq.getStatus());
        return R.ok(sysMenuService.page(page, lambdaQueryWrapper));
    }

    @Operation(summary = "根据id查询菜单信息", description = "根据id查询菜单信息", tags = "查询")
    @GetMapping("{id}")
    public R<SysMenu> getById(@PathVariable("id") Long id) {
        return R.ok(sysMenuService.getById(id));
    }

    @Operation(summary = "新增菜单", description = "新增菜单", tags = "新增")
    @PostMapping()
    public R<Boolean> add(@RequestBody @Validated SysMenu sysMenu) {
        return R.ok(sysMenuService.save(sysMenu));
    }

    @Operation(summary = "编辑菜单", description = "编辑菜单", tags = "编辑")
    @PutMapping
    public R<Boolean> edit(@RequestBody SysMenu sysMenu) {
        return R.ok(sysMenuService.updateById(sysMenu));
    }

    @Operation(summary = "根据id删除菜单", description = "根据id删除菜单", tags = "删除")
    @DeleteMapping("{id}")
    public R<Boolean> deleteById(@PathVariable("id") Long id) {
        return R.ok(sysMenuService.removeById(id));
    }

    @Operation(summary = "获取路由信息", description = "获取路由信息", tags = "查询")
    @GetMapping("getRouters")
    public R<List<SysMenuResp>> getRouters(@UserId Long userId) {
        // 超级管理员
        if (userId == 1L) {
            userId = null;
        }
        List<SysMenu> menuList = sysMenuService.getMenusByUserId(userId);
        return R.ok(sysMenuService.buildMenuTree(menuList));
    }

    @Operation(summary = "根据角色ID查询菜单下拉树结构", description = "根据角色ID查询菜单下拉树结构", tags = "查询")
    @GetMapping("roleMenuTreeSelect/{id}")
    public R<Map<String, Object>> roleMenuTreeSelectById(@PathVariable("id") Long roleId, @UserId Long userId) {
        Map<String, Object> map = new HashMap<>();
        // 超级管理员
        if (userId == 1L) {
            userId = null;
        }
        List<SysMenu> menuList = sysMenuService.getMenusByUserId(userId);
        List<SysMenuResp> menus = sysMenuService.buildMenuTree(menuList);

        map.put("checkedKeys", sysMenuService.selectMenuListByRoleId(roleId));
        map.put("menus", menus);
        return R.ok(map);
    }
}
